package com.destinomusical.epura;

import java.io.File;
import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.destinomusical.epura.adapter.VideolistAdapter;
import com.destinomusical.epura.base.BaseActivity;
import com.smartthinking.destinomusical.R;

public class MisVideos extends BaseActivity {

	VideolistAdapter  adaptador;
	String path="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.videos_list);
		setTopBar(0, 0, R.string.boton_videos);
		File mFile = new File(Environment.getExternalStorageDirectory()
				+ "/DestinoMusical");
		String[] tempList = mFile.list();
		String[]myList = filter(tempList);
		path = Environment.getExternalStorageDirectory() + "/DestinoMusical/";

		adaptador = new VideolistAdapter(this, myList,path);

		ListView lstreproduccion = (ListView) findViewById(R.id.videolist);
		lstreproduccion.setAdapter(adaptador);
		lstreproduccion.setOnItemClickListener(listener);
		

	}
	
	public String[] filter(String[] lista){
		ArrayList<String> temp = new ArrayList<String>();
		for(String item:lista){
			if(item.endsWith(".mp4"))
			temp.add(item);
		}
		int listSize = temp.size();
		String [] list = new String[listSize];
		for(int i=0; i<listSize; i++){
			list[i]=temp.get(i);
		}
		return list;
	}

	OnItemClickListener listener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			/*String extension = MimeTypeMap.getFileExtensionFromUrl(url);
			String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
			Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
			mediaIntent.setDataAndType(Uri.parse(url), mimeType);
			startActivity(mediaIntent);*/
			String movieurl = path+adaptador.getItem(position);
			Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
			intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
			startActivity(intentToPlayVideo);
			
		}};
	public void onClickButtonExplorarAtras(View view) {

		startActivity(new Intent(getApplicationContext(), MainActivity.class));

		finish();

	}

	public void onClickButtonVideoAtras(View view) {

		startActivity(new Intent(getApplicationContext(), MainActivity.class));
		setTopBar(0, 0, R.string.boton_videos);

		finish();

	}

}